package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.utils.POIUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {

        try {
            //解析excelFile文件
            List<String[]> list = POIUtils.readExcel(excelFile);

            //将List<String[]>转换成一个List<OrderSetting>集合
//        List<OrderSetting> orderSettingList = new ArrayList<>();
//
//        for (String[] strings : list) {
//            OrderSetting orderSetting = new OrderSetting();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//            Date date= null;
//            try {
//                date=sdf.parse(strings[0]);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            orderSetting.setOrderDate(date);
//            orderSetting.setNumber(Integer.parseInt(strings[1]));
//            orderSettingList.add(orderSetting);
//
//        }

            List<OrderSetting> orderSettingList = list.stream().map(arr -> {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = null;
                        try {
                            date = sdf.parse(arr[0]);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    return new OrderSetting(date, Integer.parseInt(arr[1]),0);
                    }
            ).collect(Collectors.toList());

            //调用业务将预约设置信息保存到数据库中
            ordersettingService.add(orderSettingList);


            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    @GetMapping("/getOrdersettingByMonth")
    public Result getOrdersettingByMonth(@DateTimeFormat(pattern = "yyyy-MM") Date date){    //由于没有日期，默认日期就为1号的0时0分0秒

        try {
            List<Map> list= ordersettingService.getOrdersettingByMonth(date);
            return new Result(true,"查询预约成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询预约失败");
        }
    }


    @PutMapping("/updateNumberByDate")
    public Result updateNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            ordersettingService.updateNumberByDate(orderSetting);
            return new Result(true,"修改预约信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改预约信息失败");
        }
    }




    @GetMapping("/getOrdersettingByMonth1")
    public Result getOrdersettingByMonth1(String  date){    //由于没有日期，默认日期就为1号的0时0分0秒

        try {
            List<Map> list= ordersettingService.getOrdersettingByMonth1(date);
            return new Result(true,"查询预约成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询预约失败");
        }
    }
}
